<template>
  <el-dialog
    title="更改角色"
    :visible.sync="dialogVisible"
    :before-close="closeDialog"
    class
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <!-- <div class="title-gra plr30">
      <div class="grab gra-r">
        <span class="grab gra-l"></span>
      </div>
    </div> -->
    <!--
        <div class="enc-role">
            <div class="enc-role-left">
               <div class="role-title">
                   选择角色名
               </div>
               <div>
                    <el-checkbox-group v-model="checkList" class="enc-role-check" @change="handleCheckedHandel">
                        <div v-for="(val, index) in roleData" :key="index"><el-checkbox :label="val.name"></el-checkbox></div>
                    </el-checkbox-group>
               </div>
            </div>
            <div class="enc-role-right">
                <div class="role-title">
                   已选择
                </div>
                <div class="enc-role-check">
                    <div v-for="(val, index) in checkList" :key="index">{{val}}</div>
                </div>
            </div>
        </div>
    -->
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" v-loading="loading">
      <el-form-item label="选择角色：">
        <el-select v-model="ruleForm.roleValue">
          <el-option
            v-for="item in roleOptions"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="mr-btn mt-20">
      <el-button  class="def-btn-sty"  @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addRole">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addUserRole } from "@/api/userApi.js";
import { getRoleList } from "@/api/roleApi.js";
export default {
  data() {
    return {
      userId: "",
      loading: false,
      dialogVisible: false,
      checkList: [],
      ruleForm: {
        roleValue: ""
      },
      roleOptions: []
    };
  },
  methods: {
    openDialogVisible(row) {
      this.dialogVisible = true;
      if (row) {
        this.userId = row.id;
        this.ruleForm.roleValue =
          row.roles[0] && row.roles[0].id ? row.roles[0].id : "";
      }

      this.getRoleInfo();
    },
    closeDialog() {
      this.$emit("callBack");
      this.dialogVisible = false;
    },
    getRoleInfo() {
      let params = {
        search: "",
        isValid: true,
        page: 1,
        per_page: 10000
      };
      this.loading = true;
      getRoleList(params).then(res => {
        this.loading = false;
        this.roleOptions = [];

        res.list.forEach(res => {
          this.roleOptions.push({
            id: res.id,
            roleName: res.roleName
          });
        });
        this.ruleForm.roleValue =
          !this.ruleForm.roleValue && this.roleOptions.length > 0
            ? this.roleOptions[0].id
            : "";
      });
    },
    handleCheckedHandel(val) {
      console.log("val", val);
    },
    addRole() {
      addUserRole(this.userId, this.ruleForm.roleValue).then(res => {
        if (res.data.success) {
          this.$message.success("操作成功");
          this.closeDialog();
          this.$emit("roleCallBack");
        } else {
          this.$message.error(res.data.message);
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.enc-role {
  display: flex;
  width: 100%;
}
.enc-role-left {
  width: 50%;
  border: 1px solid #cce;
  min-height: 200px;
}
.enc-role-right {
  width: 50%;
  border: 1px solid #cce;
  margin-left: 20px;
  min-height: 200px;
}
.role-title {
  background-color: #4a60a2;
  border-color: #4a60a2;
  height: 40px;
  color: #fff;
  line-height: 40px;
  padding-left: 20px;
}
.enc-role-check {
  padding: 20px;
  div {
    margin: 0 0 20px 0;
  }
}
</style>
